home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / GLUT-3.7 / PROGS / ADVANCED / genmipmap.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-12  |  3.6 KB  |  193 lines

  1.  
  2. /* genmipmap.c - by David Blythe, SGI */
  3.  
  4. /* Example of how to generate texture mipmap levels with the
  5.    accumulation buffer. */
  6.  
  7. /* Usage example: genmipmap [file.rgb] */
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <math.h>
  12. #include <GL/glut.h>
  13. #include "texture.h"
  14.  
  15. static int w = 512, h = 512;
  16. static int pause;
  17.  
  18. void 
  19. reshape(int w, int h)
  20. {
  21.   glViewport(0, 0, w, h);
  22.   glMatrixMode(GL_PROJECTION);
  23.   glLoadIdentity();
  24.   glOrtho(0, w, 0, h, -1, 1);
  25.   glMatrixMode(GL_MODELVIEW);
  26.   glLoadIdentity();
  27. }
  28.  
  29. void 
  30. init_textures(char *filename)
  31. {
  32.   unsigned *buf;
  33.   int width, height, components;
  34.  
  35.   if (filename) {
  36.     buf = read_texture(filename, &width, &height, &components);
  37.     if (buf == NULL) {
  38.       fprintf(stderr, "Error: Can't load image file \"%s\".\n",
  39.         filename);
  40.       exit(1);
  41.     } else {
  42.       printf("%d x %d texture loaded\n", width, height);
  43.     }
  44.   } else {
  45.     int i, j;
  46.     GLubyte *p;
  47.     components = 4;
  48.     width = height = 512;
  49.     buf = (unsigned *) malloc(width * height * sizeof(unsigned));
  50.     p = (GLubyte *) buf;
  51.     for (j = 0; j < height; j++) {
  52.       for (i = 0; i < width; i++) {
  53.         if (i & 1)
  54.           p[4 * (i + j * width) + 0] = 0xff;
  55.         else
  56.           p[4 * (i + j * width) + 1] = 0xff;
  57.         if (j & 1)
  58.           p[4 * (i + j * width) + 2] = 0xff;
  59.       }
  60.     }
  61.   }
  62.  
  63.   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  64.   glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  65.   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  66.   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  67.   glTexImage2D(GL_TEXTURE_2D, 0, components, width,
  68.     height, 0, GL_RGBA, GL_UNSIGNED_BYTE,
  69.     buf);
  70.   free(buf);
  71. }
  72.  
  73. void 
  74. init(char *filename)
  75. {
  76.   glEnable(GL_TEXTURE_2D);
  77.   init_textures(filename);
  78. }
  79.  
  80. void 
  81. draw_rect(int w, int h)
  82. {
  83.   glBegin(GL_QUADS);
  84.   glTexCoord2f(0, 0);
  85.   glVertex2f(0, 0);
  86.   glTexCoord2f(1, 0);
  87.   glVertex2f(w, 0);
  88.   glTexCoord2f(1, 1);
  89.   glVertex2f(w, h);
  90.   glTexCoord2f(0, 1);
  91.   glVertex2f(0, h);
  92.   glEnd();
  93. }
  94.  
  95. void 
  96. stop(void)
  97. {
  98.   if (pause) {
  99.     printf("? ");
  100.     fflush(stdout);
  101.     getchar();
  102.   }
  103. }
  104.  
  105. void 
  106. acfilter(int width, int height)
  107. {
  108.   glClear(GL_COLOR_BUFFER_BIT | GL_ACCUM_BUFFER_BIT);
  109.   glMatrixMode(GL_TEXTURE);
  110.  
  111.   if (pause) {
  112.     draw_rect(width, height);
  113.     stop();
  114.     glClear(GL_COLOR_BUFFER_BIT);
  115.   }
  116.   draw_rect(width / 2, height / 2);
  117.   stop();
  118.   glAccum(GL_ACCUM, 0.25);
  119.  
  120.   glTranslatef(1.0 / width, 0, 0);
  121.   draw_rect(width / 2, height / 2);
  122.   stop();
  123.   glAccum(GL_ACCUM, 0.25);
  124.  
  125.   glLoadIdentity();
  126.   glTranslatef(0, 1.0 / height, 0);
  127.   draw_rect(width / 2, height / 2);
  128.   stop();
  129.   glAccum(GL_ACCUM, 0.25);
  130.  
  131.   glLoadIdentity();
  132.   glTranslatef(1.0 / width, 1.0 / height, 0);
  133.   draw_rect(width / 2, height / 2);
  134.   stop();
  135.   glAccum(GL_ACCUM, 0.25);
  136.  
  137.   glAccum(GL_RETURN, 1.0);
  138.   glMatrixMode(GL_MODELVIEW);
  139. }
  140.  
  141. void 
  142. display(void)
  143. {
  144.   glClear(GL_COLOR_BUFFER_BIT);
  145.   draw_rect(w, h);
  146.   acfilter(w, h);
  147.   glFlush();
  148. }
  149.  
  150. void 
  151. help(void)
  152. {
  153.   printf("'h'   - help\n");
  154.   printf("'s'   - toggle single step mode\n");
  155. }
  156.  
  157. /* ARGSUSED1 */
  158. void
  159. key(unsigned char key, int x, int y)
  160. {
  161.   switch (key) {
  162.   case 'h':
  163.     help();
  164.     break;
  165.   case 's':
  166.     pause ^= 1;
  167.     break;
  168.   case '\033':
  169.     exit(0);
  170.     break;
  171.   default:
  172.     glutPostRedisplay();
  173.   }
  174. }
  175.  
  176. int 
  177. main(int argc, char *argv[])
  178. {
  179.   glutInit(&argc, argv);
  180.   glutInitWindowSize(w, h);
  181.   glutInitDisplayMode(GLUT_RGBA | GLUT_ACCUM);
  182.   (void) glutCreateWindow("genmipmap");
  183.   if (argc > 1)
  184.     init(argv[1]);
  185.   else
  186.     init(0);
  187.   glutDisplayFunc(display);
  188.   glutKeyboardFunc(key);
  189.   glutReshapeFunc(reshape);
  190.   glutMainLoop();
  191.   return 0;             /* ANSI C requires main to return int. */
  192. }
  193.